// pages/coupon/coupon.js
var Zan = require('../../../dist/index.js');
var coupon = require('../../../template/couponTemp.js');
var network = require('../../../utils/network_util.js');
var utils = require('../../../utils/utils.js');
var api = require('../../../utils/api.js');
var Zan = require('../../../dist/index');
var start01 = 0;
var start02 = 0;
var start03 = 0;

Page(Object.assign({}, Zan.TopTips, Zan.Tab, {

  /**
   * 页面的初始数据
   */
  data: {
    tab: {
      list: [{
        id: 0,
        title: '未使用'
      }, {
        id: 1,
        title: '已使用'
      }, {
        id: 2,
        title: '已过期'
      }],
      selectedId: 0,
      scroll: false
    },
    activeIndex: 0,
    recordsTotal01: 0,
    recordsTotal02: 0,
    recordsTotal03: 0,
    coupon01: [],
    coupon02: [],
    coupon03: [],
    nodata: {
      msg: '没有优惠券',
      notype: 2,
      imgurl: '../../../images/nodatas.png',
      nourl: ''
    },

    myself: 1

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: "优惠券",
    })

    this.onPullDownRefresh();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    if (this.data.tab.selectedId == 0) {
      start01 = 0;
      this.setData({
        coupon01: [],
      });
      network._post_page_json(this, api.getCouponList, start01, { status: this.data.tab.selectedId }, success01);
    } else if (this.data.tab.selectedId == 1) {
      start02 = 0;
      this.setData({
        coupon02: [],
      });
      network._post_page_json(this, api.getCouponList, start02, { status: this.data.tab.selectedId }, success02);
    } else if (this.data.tab.selectedId == 2) {
      start03 = 0;
      this.setData({
        coupon03: [],
      });
      network._post_page_json(this, api.getCouponList, start03, { status: this.data.tab.selectedId }, success03);
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.tab.selectedId == 0) {
      if (this.data.coupon01.length < this.data.recordsTotal01) {
        start01 = start01 + 20;
        network._post_page_json(this, api.getCouponList, start01, { status: this.data.tab.selectedId }, success01);
      }
    } else if (this.data.tab.selectedId == 1) {
      if (this.data.coupon02.length < this.data.recordsTotal02) {
        start02 = start02 + 20;
        network._post_page_json(this, api.getCouponList, start02, { status: this.data.tab.selectedId }, success02);
      }
    } else if (this.data.tab.selectedId == 2) {
      if (this.data.coupon03.length < this.data.recordsTotal03) {
        start03 = start03 + 20;
        network._post_page_json(this, api.getCouponList, start03, { status: this.data.tab.selectedId }, success03);
      }
    }

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 
   */
  handleZanTabChange(e) {
    var componentId = e.componentId;
    var selectedId = e.selectedId;

    this.setData({
      [`${componentId}.selectedId`]: selectedId
    });

    this.setData({
      activeIndex: selectedId
    })

    if (this.data.tab.selectedId == 0) {
      start01 = 0;
      this.setData({
        coupon01: [],
      });
      network._post_page_json(this, api.getCouponList, start01, { status: this.data.tab.selectedId }, success01);
    } else if (this.data.tab.selectedId == 1) {
      start02 = 0;
      this.setData({
        coupon02: [],
      });
      network._post_page_json(this, api.getCouponList, start02, { status: this.data.tab.selectedId }, success02);
    } else if (this.data.tab.selectedId == 2) {
      start03 = 0;
      this.setData({
        coupon03: [],
      });
      network._post_page_json(this, api.getCouponList, start03, { status: this.data.tab.selectedId }, success03);
    }
  },

  // 查看优惠券详情
  couponDetails: function (e) {
    coupon.couponDetails(this, e);
  },

  // 立即使用优惠券
  userCoupon: function (e) {
    coupon.userCoupon(this, e)
  },

  refresh() {
    start01 = 0;
    this.setData({
      coupon01: [],
    });
    network._post_page_json(this, api.getCouponList, start01, { status: this.data.tab.selectedId }, success01);
  }
}))

// 获取未使用优惠列表结果
var success01 = function (that, res) {
  if (res.data.data) {
    var coupon = res.data.data;
    for (var i = 0; i < coupon.length; i++) {
      coupon[i].startTime = utils.formatDate02(new Date(coupon[i].startTime))
      coupon[i].endTime = utils.formatDate02(new Date(coupon[i].endTime))
    }
    that.setData({
      coupon01: that.data.coupon01.concat(coupon),
      recordsTotal01: res.data.recordsTotal01
    })
  }
}

// 获取已使用优惠列表结果
var success02 = function (that, res) {
  if (res.data.data) {
    var coupon = res.data.data;
    for (var i = 0; i < coupon.length; i++) {
      coupon[i].startTime = utils.formatDate02(new Date(coupon[i].startTime))
      coupon[i].endTime = utils.formatDate02(new Date(coupon[i].endTime))
    }
    that.setData({
      coupon02: that.data.coupon02.concat(coupon),
      recordsTotal02: res.data.recordsTotal02
    })
  }
}
// 获取已到期优惠列表结果
var success03 = function (that, res) {
  if (res.data.data) {
    var coupon = res.data.data;
    for (var i = 0; i < coupon.length; i++) {
      coupon[i].startTime = utils.formatDate02(new Date(coupon[i].startTime))
      coupon[i].endTime = utils.formatDate02(new Date(coupon[i].endTime))
    }
    that.setData({
      coupon03: that.data.coupon03.concat(coupon),
      recordsTotal03: res.data.recordsTotal03
    })
  }
}